package com.education.module.professional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.Professional;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class ProfessionalServices extends BaseServices implements IDao<Professional>{

	@Override
	public List<Professional> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,pro_name,charge_person FROM professional");
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("proName") && !"".equals((String)searchParams.get("proName"))){
				sql.append(lp).append(" pro_name like ? ");
				argsList.add("%" + searchParams.get("proName") + "%");
				lp = " and ";
			}
		}
		Object[] args = argsList.toArray();
		List<Professional> list = dao.query(pageSQL(sql.toString(),page),args,new ProfessionalRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from professional ",new Object[]{}));
		return list;
	}
	
	public List<Professional> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,pro_name,charge_person FROM professional");
		
		List<Professional> list = dao.query(sql.toString(),new ProfessionalRowmapper());
		return list;
	}

	@Override
	public int save(Professional obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("insert into professional ( "); 
 sql.append("id,pro_name,charge_person ");  
 sql.append(" ) values(?,?,?) "); 
 Object[] args = {obj.getId(),obj.getProName(),obj.getChargePerson() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public Professional findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,pro_name,charge_person FROM professional ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new ProfessionalRowmapper());
	}

	@Override
	public void update(Professional obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update professional "); 
 sql.append("set  "); 
 sql.append("pro_name=?,charge_person=? where id=?  "); 
 Object[] args = {obj.getProName(),obj.getChargePerson(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from professional where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class ProfessionalRowmapper implements RowMapper<Professional> {
		@Override
		public Professional mapRow(ResultSet rs, int arg1) throws SQLException {
			Professional obj = new Professional();
			obj.setProName(rs.getString("pro_name")); 
obj.setId(rs.getInt("id")); 
obj.setChargePerson(rs.getString("charge_person")); 

			return obj;
		}
	}

}
